package com.letoy.study.controller;

import com.letoy.study.entity.PreRegisterUser;
import com.letoy.study.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

import static com.letoy.study.utils.Constants.*;

@RestController
@Api(value = "实现用户操作的基本业务", tags = "用户业务Controller")
@RequestMapping(value = "/user")
public class UserController {

    @Resource
    UserService userService;

    @ApiOperation(value = "注册用户", notes = "根据email和password预注册用户")
    @RequestMapping(value = "/preRegisterUser", method = RequestMethod.POST)
    private Map<String, Object> preRegisterUser(PreRegisterUser preRegisterUser) {
        HashMap<String, Object> map = new HashMap<>();
        String activeCode = userService.preRegisterUser(preRegisterUser);
        if (activeCode != null) {
            if (activeCode.equals("EXIST")) {
                map.put("status", FAIL);
                map.put("msg", "该用户已经注册");
            } else {
                map.put("status", SUCCESS);
                map.put("activeCode", activeCode);
            }
        } else {
            map.put("status", EXCEPTED_SYSTEM_ERROR);
        }
        return map;
    }

    @ApiOperation(value = "激活用户", notes = "根据email和activeCode预注册用户")
    @RequestMapping(value = "/activeUser", method = RequestMethod.POST)
    private HashMap<String, Object> activeUser(String email, String activeCode) {
        System.out.println(email + activeCode);
        return userService.activeUser(email, activeCode);
    }

}
